import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';
import SharedModule from './shared'

reportWebVitals();

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  ReactDOM.render(<App />, document.getElementById('react-root'));
}

export async function bootstrap() {
  console.log('react app bootstraped');
}

export async function mount(props) {
  console.log('props from main framework', props);
  const { container } = props
  // 当传入的 shared 为空时，使用子应用自身的 shared
  // 当传入的 shared 不为空时，主应用传入的 shared 将会重载子应用的 shared
  if (props) {
    const { shared = SharedModule.getShared() } = props;
    SharedModule.overloadShared(shared);
  }
  ReactDOM.render(<App />, container ? container.querySelector('#react-root') : document.getElementById('react-root'));
}

export async function unmount() {
  const node = document.getElementById('react-root');
  if (node) {
    ReactDOM.unmountComponentAtNode(node);
  }
}